import { USER_ADDRESS_LIST,USER_ADDRESS_DELETEADDRESS } from "../../../messages/uris";
import req from "../../../utils/request";
// pages/adsfas.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tipmessage:"您还没有保存过地址",//没有地址时的提示消息
    addressList:[],
    addressSize:0, 
    //弹出层
    topTips: false,
    hide: false,
    tipValue: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.selectList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  //查询个人全部地址
  selectList:function(){
    const that = this;
    req.get(USER_ADDRESS_LIST,{ pageSize: 10, pageNo: 1 },
      (resp) => {
        that.setData({
          addressList:resp.data.result.records,
          addressSize:resp.data.result.total,
         });
      },
      (resp) => {
        console.log("查询地址失败");
      },(res)=>{},that
    );
  },

  //删除地址
  remove:function(e){
    var addressid = e.currentTarget.dataset.addressid
    const that = this;
    req.get(USER_ADDRESS_DELETEADDRESS + "?addressId=" +addressid,{},
      (resp) => {
        wx.showToast({
          title: '删除地址成功',
          icon: '', 
          duration: 2000, 
        })
        that.selectList();
      },
      (resp) => {
        wx.showToast({
          title: '删除地址失败',
          icon: '',
          duration: 2000, 
        })
      },(res)=>{},that
    );
  },
  
  //进入添加页面
  tosenderaddress:function(){
    wx.navigateTo({
      url: '/pages/sender/sender',
    })
  },

  //进入修改页面
  openAddressUpdate:function(e){
    var address = e.currentTarget.dataset.address;
    address = JSON.stringify(address) //将对象转为string
    wx.navigateTo({
      url: '/pages/sender/sender?address='+address,
    })
  },

  //选择偶然地址
  defaultAddress:function(item){
    var address = item.currentTarget.dataset.address;
    address.username = address.addressName
    address.phone = address.addressPhone
    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 2];
    prevPage.setData({ 
      address: address,
    })
    wx.navigateBack({//返回上一页  
      delta:1
    })
  }

  
})